package com.atguigu.gmall.product.api;

import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.starter.redisson.properties.BloomFilterProperties;
import com.atguigu.starter.redisson.properties.BloomProperty;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/14 0:21
 */
@RequestMapping("/api/bloom")
@RestController
public class BloomFilterController {

    @Autowired
    private RedissonClient client;
    @Autowired
    private SkuInfoService skuInfoService;
//    @Autowired
//    private BloomFilterProperties bloomFilterProperties;
    @Qualifier("skuBloomFilte")
    @Autowired
    private RBloomFilter<Object> rBloomFilter;
    /**
     * 模拟初始化布隆过滤器
     */
    @RequestMapping("/init")
    public String initBloom(){
        //尝试对过滤器进行初始化
        rBloomFilter.tryInit(1000000,0.0001);
        return "ok";
    }

    /**
     * 模拟给布隆过滤器添加数据
     */
    @RequestMapping("/add/{skuId}")
    public String add(@PathVariable String skuId){
        boolean add = rBloomFilter.add(skuId);
        return "ok";
    }

    /**
     * 模拟查询布隆过滤器是否存在某个数据
     */
    @RequestMapping("/exist/{skuId}")
    public String existKey(@PathVariable String skuId){
        return rBloomFilter.contains(skuId)+"----->数据库有没有";
    }

    /**
     * 模拟重建布隆过滤器
     */
    @RequestMapping("/rebuild/{bloomName}")
    public String rebuildBloom(@PathVariable("bloomName") String bloomName){
        skuInfoService.rebuildSkuBloomFilter();
        return bloomName+"布隆过滤器重建完成!";
    }
}
